package com.lzy.product.common.util;

import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
    /**
     * 获得该月第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份  
        cal.set(Calendar.YEAR, year);
        //设置月份  
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数  
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数  
        cal.set(Calendar.DAY_OF_MONTH, firstDay);

        return cal.getTime();
    }

    /**
     * 获得该月最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份  
        cal.set(Calendar.YEAR, year);
        //设置月份  
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数  
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数  
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期  
        return cal.getTime();
    }

    public static int daysBetween(int months) {
        Calendar calendar = Calendar.getInstance();
        Date startTime = calendar.getTime();
        calendar.add(Calendar.MONTH, months);
        Date endTime = calendar.getTime();
        int days = ((int) (endTime.getTime()/ 1000) - (int) (startTime.getTime() / 1000)) / 3600 / 24;
        return days;
    }
}
